package com.itheima.search.controller;

import com.itheima.search.model.constant.MessageConstant;
import com.itheima.search.model.entity.PageResult;
import com.itheima.search.model.entity.QueryPageBean;
import com.itheima.search.model.entity.Result;
import com.itheima.search.model.pojo.CheckGroup;
import com.itheima.search.model.pojo.CheckItem;
import com.itheima.search.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lee
 * @created 2024/5/11
 * @describtion
 */
@RestController
@RequestMapping("/checkitem")
@Api("检查项管理接口")
@Slf4j
public class CheckitemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/add.do")
    @ApiOperation("新增检查项")
    public Result add(@RequestBody CheckItem checkItem) {
        log.info("新增检查项");
        try {
            checkItemService.save(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 删除检查项
     *
     * @return
     */
    @GetMapping("/delete.do")
    public Result delCheckItem(Integer id) {
        log.info("删除检查项");
        try {
            checkItemService.delItem(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 更新检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/edit.do")
    @ApiOperation("更新检查项")
    public Result update(@RequestBody CheckItem checkItem) {
        log.info("更新检查项");
        try {
            checkItemService.update(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    /**
     * 查询所有的检查项
     *
     * @return
     */
    @GetMapping("/findAll.do")
    @ApiOperation("查询所有的检查项")
    public Result findAll() {
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
    }

    /**
     * 根据ID查询检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/findById.do")
    @ApiOperation("根据ID查询检查项")
    public Result findById(Integer id) {
        log.info("根据ID查询检查项");
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 检查项分页查询-条件查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage.do")
    @ApiOperation("检查项分页查询-条件查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("检查项分页查询-条件查询");
        PageResult pageResult = checkItemService.page(queryPageBean);
        return pageResult;
    }


}
